package tech.eyao.hs60x.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wuzhengfei358
 */
public class DateUtil {
    public static final String           DATE_FORMAT_STR     = "yyyyMMdd";
    public static final String           DATE_FORAMT         = "yyyy-MM-dd";
    public static final String           TIME_HOUR_FORAMT    = "HH:mm";
    public static final String           TIME_FORAMT         = "HH:mm:ss";
    public static final String           TIME_MS_FORAMT      = "HH:mm:ss:SSS";
    public static final String           RANKING_DATE_STR    = "yyyy/MM/dd";
    public static final String           MONTH_DAY_FORMAT    = "MM月dd日";
    public static final String           LOTTERY_DATE_FORMAT = "yyyy.MM.dd";
    private static Logger                logger              = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 日期+时间格式，不包括毫秒
     */
    public static final String           DATE_TIME_FORMAT    = DATE_FORAMT + " " + TIME_FORAMT;
    /**
     * 日期+时间格式，包括毫秒
     */
    public static final String           DATE_TIME_MS_FORMAT = DATE_FORAMT + " " + TIME_MS_FORAMT;

    public static final SimpleDateFormat DAY_FORMATOR        = new SimpleDateFormat(DATE_FORAMT);
    public static final SimpleDateFormat DATE_TIME_FORMATOR  = new SimpleDateFormat(DATE_TIME_FORMAT);

    
    /**
     * 指定日期的开始时间（至 00:00:00）
     */
    public static Date getStartTime(Date date) {
        return DateUtils.addDays(DateUtils.ceiling(date, Calendar.DATE), -1);
    }
    
    /**
     * 指定日期的结束时间（至 23:59:59）
     */
    public static Date getEndTime(Date date) {
        return DateUtils.addSeconds(DateUtils.ceiling(date, Calendar.DATE), -1);
    }

    /**
     * 指定日期的结束时间（至 23:59:59）
     */
    public static Date getEndTime(long timestamp) {
        return getEndTime(new Date(timestamp));
    }

    /**
     * <p>
     * 距离当天结束剩余的秒数 （当天 23:59:59）
     * </p>
     * <p>
     * 单位:秒
     * </p>
     */
    public static long getTodayRemainingSeconds() {
        long remaining = (getEndTime(LocalDate.now().toDate()).getTime() - System.currentTimeMillis()) / 1000;
        // 如果隔天，则当天剩余时间为 0
        return remaining > 0 ? remaining : 1l;
    }

    public static double getTimestamp(long timestamp, int second) {
        LocalDateTime localDateTime = new LocalDateTime(timestamp);
        localDateTime = localDateTime.plusSeconds(second);
        return localDateTime.toDate().getTime();
    }

    public static Date getFormatDate() {
        return LocalDate.now().toDate();
    }

    public static Date addDay(int day) {
        return LocalDate.now().plusDays(day).toDate();
    }

    /**
     * 将Date日期转换为String
     * 
     * @param date
     * @param formatStr
     * @return
     */
    public static String date2String(Date date, String formatStr) {
        if (null == date || null == formatStr) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat(formatStr);

        return df.format(date);
    }

    /**
     * 将String日期转换为String
     * 
     * @param date
     * @param formatStr
     * @return
     * @throws ParseException
     */
    public static String date2String(String date, String formatStr) {
        if (null == date || null == formatStr) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat(formatStr);
        Date dateTime;
        try {
            dateTime = df.parse(date);
            return df.format(dateTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static Date parseDate(String dateStr, String format) {
        if (dateStr == null || dateStr.trim().length() == 0) {
            return null;
        }
        try {
            return new SimpleDateFormat(format).parse(dateStr);
        } catch (ParseException e) {
            logger.error("", e);
            return null;
        }
    }

}
